package com.sfy.yxy.doctor.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sfy.yxy.doctor.R;
import com.sfy.yxy.doctor.base.BaseActivity;

import org.greenrobot.eventbus.Subscribe;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;


/**
 * @author :creat by Xia燚
 * 时间：2021/9/8
 * 邮箱：XiahaotianV@163.com
 *
 **/
public class SplashActivity extends BaseActivity {


    @BindView(R.id.tv_context)
    TextView tvContext;
    @BindView(R.id.tv_number)
    TextView tvNumber;
    @BindView(R.id.ll_number)
    LinearLayout llNumber;
    private Typeface typeface;
    long elapsed = 5000;
    final static long INTERVAL = 1000;
    private Timer timer;
    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView() {

        typeface = Typeface.createFromAsset(getAssets(), "Hy_by.ttf"); //加载第三方字体
        tvContext.setTypeface(typeface); //设置第三方字体
    }

    @Override
    protected void initData() {
        ShowCountDownTime();
        llNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (timer != null) {
                    timer.cancel();
                }
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }


    @Subscribe
    public void onEvent(String event) {

    }


    private void ShowCountDownTime() {
        displayText(elapsed / 1000 + "");
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                elapsed -= INTERVAL;
                if (elapsed < 0) {
                    this.cancel();
                    new Handler(Looper.getMainLooper()).post(new Runnable() {
                        @Override
                        public void run() {
                            //执行完毕
                            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                    return;
                }
                displayText(elapsed / 1000 + "");
            }
        };
        timer = new Timer();
        timer.schedule(task, INTERVAL, INTERVAL);
    }

    private void displayText(final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                switch (text){
                    case "4":
                        llNumber.setBackgroundResource(R.mipmap.count_down4);
                        break;
                    case "3":
                        llNumber.setBackgroundResource(R.mipmap.count_down3);
                        break;
                    case "2":
                        llNumber.setBackgroundResource(R.mipmap.count_down2);
                        break;
                    case "1":
                        llNumber.setBackgroundResource(R.mipmap.count_down1);
                        break;
                    case "0":
                        llNumber.setBackgroundResource(R.mipmap.count_down0);
                        break;
                }
                if(text.equals("5")){
                    return;
                }
                tvNumber.setText(text);
            }
        });
    }







}
